package com.scribbles.common.entity;

// Generated 15-Jul-2011 16:49:40 by Hibernate Tools 3.4.0.CR1

import java.util.Date;
import java.util.List;



import javax.persistence.Column;
import javax.persistence.Entity;

import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import javax.persistence.Version;

/**
 * ArticleCategory generated by hbm2java
 */
@Entity
@Table(name = "article_category", catalog = "ksharedb")
@NamedQueries({
	@NamedQuery(name="getMainCategoriesForUser", query="select ac from ArticleCategory ac where ac.parentCategory = null and ac.appUser.id=:userId")
})
public class ArticleCategory implements java.io.Serializable {

	private Integer id;
	private Integer version;
	private String name;
	private Integer createdBy;
	private Integer updatedBy;
	private Date createdDate;
	private Date updatedDate;
	private ArticleCategory parentCategory;
	private List<ArticleCategory> childCategories;
	private AppUser appUser;

	public ArticleCategory() {
	}

	public ArticleCategory(int id) {
		this.id = id;
	}

	public ArticleCategory(int id, String name, Integer createdBy,
			Integer updatedBy, Date createdDate, Date updatedDate) {
		this.id = id;
		this.name = name;
		this.createdBy = createdBy;
		this.updatedBy = updatedBy;
		this.createdDate = createdDate;
		this.updatedDate = updatedDate;
	}

	@Id
	@Column(name = "ID", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Version
	@Column(name = "VERSION")
	public Integer getVersion() {
		return this.version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	@Column(name = "NAME", length = 45)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@OneToOne
	@JoinColumn(name="APP_USER_ID")
	public AppUser getAppUser() {
		return appUser;
	}
	public void setAppUser(AppUser appUser) {
		this.appUser = appUser;
	}

	@ManyToOne
	@JoinColumn(name = "PARENT_CATEGORY_ID")
	public ArticleCategory getParentCategory() {
		return parentCategory;
	}

	public void setParentCategory(ArticleCategory parentCategory) {
		this.parentCategory = parentCategory;
	}

	@OneToMany(mappedBy = "parentCategory")
	public List<ArticleCategory> getChildCategories() {
		return childCategories;
	}

	public void setChildCategories(List<ArticleCategory> childCategories) {
		this.childCategories = childCategories;
	}

	@Column(name = "CREATED_BY")
	public Integer getCreatedBy() {
		return this.createdBy;
	}

	public void setCreatedBy(Integer createdBy) {
		this.createdBy = createdBy;
	}

	@Column(name = "UPDATED_BY")
	public Integer getUpdatedBy() {
		return this.updatedBy;
	}

	public void setUpdatedBy(Integer updatedBy) {
		this.updatedBy = updatedBy;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATED_DATE", length = 19)
	public Date getCreatedDate() {
		return this.createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "UPDATED_DATE", length = 19)
	public Date getUpdatedDate() {
		return this.updatedDate;
	}

	public void setUpdatedDate(Date updatedDate) {
		this.updatedDate = updatedDate;
	}

	
	public String toJson() {
		StringBuffer sb = new StringBuffer(32);
		sb.append('{');
		sb.append("\"id\"").append(':').append('"').append(getId())
				.append('"').append(',');
		sb.append("\"text\"").append(':').append('"').append(getName())
		.append('"').append(',');
		if (null != this.childCategories && childCategories.size() > 0) {
			sb.append("\"leaf\"").append(':').append("false").append(',');
			sb.append("\"children\"").append(':').append('[');
			int count = 1;
			int childrenSize = childCategories.size();
			for (ArticleCategory child : childCategories) {
				sb.append(child.toJson());
				if (count++ != childrenSize) {
					sb.append(',');
				}
				// appendedNodes.putAll(child.getAppendedNodes());
			}
			sb.append(']').append(',');
		} else {
			sb.append("\"leaf\"").append(':').append("true").append(',');
		}

		sb.append("\"expanded\"").append(':').append('"').append("true")
				.append('"');
		sb.append('}');
		// appendedNodes.put(id, this);
		return sb.toString();
	}
	
}
